#include "cuimakerrender.h"
#include "HGEDevice.h"
#include <QTimer>
#include "hgesprite.h"
#include <hgefont.h>

#include <WndDesktop.h>
#include <WndBase.h>

//CWndDesktop* g_pDesktop = NULL;
#include <QMouseEvent>
#define HEART_BEAT_TICKS 25
CUIMakerRender::CUIMakerRender(QWidget *parent)
	: QWidget(parent)
	,m_pCursorInWnd(NULL)
{
	setAttribute( Qt::WA_PaintOnScreen, true );

	QTimer* timer = new QTimer(this);
	connect( timer, SIGNAL(timeout()),this,SLOT(update()));
	connect( timer, SIGNAL(timeout()),this,SLOT(slHeartBeat()));
	timer->start( HEART_BEAT_TICKS );

	setMouseTracking( true );
	g_pDevice = new HGEDevice;
	//hgeSprite::hge = g_pDevice->m_pHge;
	g_pDevice->Init(winId());

	resize(SCREEN_WIDTH,SCREEN_HEIGHT);

	g_pDesktop = new CWndDesktop;
	g_pDesktop->Create(0,0,1024,700,0,0);
	g_pDesktop->m_bEditState = true;

	//g_pDesktop->m_pHgeFont = new hgeFont("fontScore.fnt");
	//g_pDesktop->IsVisible();
	g_pDesktop->SetFont("fontScore.fnt");

	int a = 0;

//  	CWndBase* pWnd = new CWndBase;
//  	pWnd->Create(0,0,100,100,g_pDesktop,0);
//  	pWnd->ShowBox(true);
// 
//  	CWndButton* pChatBird = new CWndButton;
//  	pChatBird->Create(0,0,60,40,"EntityPic.png",pWnd,115);
// //  	pChatBird->AddRes(0,378,33,33);
// //  	pChatBird->AddRes(0,410,33,33);
// //  	pChatBird->AddRes(0,505,33,33);
// //  	pChatBird->AddRes(0,543,33,33);
// //  	pChatBird->ShowBox(true);
// 
//  	CWndStatic* m_pStatic = new CWndStatic;
//  	m_pStatic->CreateNoFont(100,0,100,14,"afdsfs",pWnd,0);
//  	m_pStatic->ShowBox(true);
}

CUIMakerRender::~CUIMakerRender()
{
	g_pDesktop->Destroy();
	g_pDevice->Release();
}

QPaintEngine* CUIMakerRender::paintEngine() const
{
	return NULL;
}

void CUIMakerRender::paintEvent( QPaintEvent * )
{
	if( updatesEnabled() )
	{
		g_pDevice->Gfx_BeginScene();

		g_pDesktop->OnDraw();

		g_pDevice->Gfx_EndScene(winId() );
	}
}

void CUIMakerRender::slHeartBeat()
{
	if( g_pDesktop )
	{
		g_pDesktop->OnUpdate( HEART_BEAT_TICKS );
		g_pDesktop->OnMouseInput();
	}
}

void CUIMakerRender::mouseMoveEvent( QMouseEvent* event )
{
	int nCurX = event->x();
	int nCurY = event->y();

	if( m_pCursorInWnd )
	{
		if( m_pCursorInWnd != g_pDesktop )
		{
			int nWndCurX = m_pCursorInWnd->GetClientPos().x;
			int nWndCurY = m_pCursorInWnd->GetClientPos().y;

			int nDiffX = nCurX - m_nPrevX ;
			int nDiffY = nCurY - m_nPrevY ;
			m_pCursorInWnd->MoveWindow( nWndCurX + nDiffX, nWndCurY + nDiffY );
		}
	}

	m_nPrevX = nCurX;
	m_nPrevY = nCurY;

	g_pDesktop->m_ptMouse.x = event->x();
	g_pDesktop->m_ptMouse.y = event->y();


	sgMouseMove( event );
}

void CUIMakerRender::mousePressEvent( QMouseEvent * event )
{
	if( event->button() == Qt::LeftButton )
	{
		m_pCursorInWnd = g_pDesktop->m_pWndCursorIn;
	}
	sgMousePressEvent( event );
}

void CUIMakerRender::mouseReleaseEvent( QMouseEvent * event )
{
	if( event->button() == Qt::LeftButton )
	{
		m_pCursorInWnd = NULL;
	}
	sgMouseReleaseEvent( event );
}
